# 生成器计算平均数
# def my_avg(num):
#     total=0
#     cishu=0
#     while cishu<=num:
#         total+=cishu
#         cishu+=1
#     yield total/cishu
#
# result=my_avg(3)
# print(next(result))


# 协程计算平均数
def coro_avg():
    total = 0
    length = 0
    while True:
        try:
            value = yield total/length
        except ZeroDivisionError:
            value = yield 0
        total += value
        length += 1

my_avg = coro_avg()
print(next(my_avg))
print(my_avg.send(2))
print(my_avg.send(3))


